package com.spring.controller.dao;

import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.spring.controller.dto.MongoTestDTO;

@Repository("testDao")
public class TestDao extends MongoDAO {
	
	private static final String TABLE_NAME = "mongo_test";
	private static final String ID = "test_id";
	private static final String ID_PROPERTY = "testId";
	
	public boolean saveTest(MongoTestDTO vo){
		return save(MongoTestDTO.class, TABLE_NAME, vo);
	}
	
	public MongoTestDTO findObjById(Long id){
		try {
			return (MongoTestDTO) find(MongoTestDTO.class, TABLE_NAME, null, "{" + ID + ":" + id + "}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean delObjById(Long id){
		DBObject query=new BasicDBObject(); 
		query.put(ID, id);
		String error = mongoTemplate.getCollection(TABLE_NAME).remove(query).getError();
		return (error == null || "".equals(error));
	}
	
	public boolean updateObj(MongoTestDTO vo){
		return updateVO(TABLE_NAME, ID_PROPERTY, vo);
	}
	
	public boolean updateObjName(Integer id,String name){
		Criteria criteria = Criteria.where(ID).is(id);
		Update update = new Update();
		update.set("type_name", name);
		
/*		String error = mongoTemplate.updateFirst(new Query(criteria), update, TABLE_NAME).getError();
		return (error == null || "".equals(error));*/
		
		String error = mongoTemplate.updateMulti(new Query(criteria), update, TABLE_NAME).getError();
		return (error == null || "".equals(error));
		
	}
}
